package repository

import (
	"gitee.com/ling1314/origin-utility/db"
	"gitee.com/ling1314/origin-utility/model"
)

type propertyRepository struct {
}

func (r propertyRepository) FindAll() (o []model.Property) {
	if db.GetDB().Find(&o).Error != nil {
		return nil
	}
	return
}

func (r propertyRepository) Create(o *model.Property) (err error) {
	err = db.GetDB().Create(o).Error
	return
}

func (r propertyRepository) UpdateByName(o *model.Property, name string) error {
	o.Name = name
	return db.GetDB().Updates(o).Error
}

func (r propertyRepository) DeleteByName(name string) error {
	return db.GetDB().Where("name = ?", name).Delete(model.Property{}).Error
}

func (r propertyRepository) FindByName(name string) (o model.Property, err error) {
	err = db.GetDB().Where("name = ?", name).First(&o).Error
	return
}

func (r propertyRepository) FindAllMap() map[string]string {
	properties := r.FindAll()
	propertyMap := make(map[string]string)
	for i := range properties {
		propertyMap[properties[i].Name] = properties[i].Value
	}
	return propertyMap
}
